// pages/detail/index.js
var WxParse = require('../../wxParse/wxParse.js');
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        showMotal: false,
        good: {},
        skuId: 0,
        buyurl: "",
        from_uid: 0,
        isDetailShow:true,
        isDetailContentShow:false,
        memberid: 0,
        globalset: {},
        isshow_subsidy:true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

       
        var that=this;
        if (typeof options.from_uid != "undefined") {
            wx.setStorage({
                key: "from_uid",
                data: options.from_uid
            });
            this.setData({
                from_uid: options.from_uid,
                datalock: false
            });
        } else {
            /*
            this.setData({
              from_uid: 1,
              datalock: false
            });
            */
        }
        if (typeof options.skuId != "undefined") {

            this.setData({
                skuId: options.skuId
            });
        } else {
            // this.setData({
            //   skuId: 16113788870
            // });
        }
        this.getGlobalSet();
        

       
        if (that.data.skuId != 0 && that.data.from_uid != 0) {
          app.util.request({
            url: 'entry/wxapp/Getcache',
            data: {
              m: "nets_haojk",
              skuId: that.data.skuId
            },
            method: 'post',
            success: function (response) {
              that.setData({
                showLoading: false,
                good: response.data.data
              });
            }
          })
        }else{
          wx.getStorage({
            key: 'good',
            success: function (res) {
              that.setData({ good: res.data });
            }
          });
        }
        

    },
    getGlobalSet: function () {
        var $this = this;
        app.util.request({
            url: 'entry/wxapp/Global',
            data: {
                "m": 'nets_haojk'
            },
            method: 'post',
            success: function (response) {
                var globalset = response.data.data;
                $this.setData({
                    globalset: globalset
                });
                wx.setStorage({
                    key: "globalset",
                    data: globalset
                });

                $this.getMember();
            }
        });

    },
    getMember: function () {
        var that = this;
        app.getUserInfo(function (userInfo) {
            //更新数据
            app.util.getUserInfo(function (response) {
                response.memberInfo.nickname = userInfo.nickName;
                response.memberInfo.avatar = userInfo.avatarUrl;
                response.memberInfo.city = userInfo.city;
                response.memberInfo.country = userInfo.country;
                response.memberInfo.province = userInfo.province;
                response.memberInfo.gender = userInfo.gender;
                that.addMember(response.memberInfo);
            });
        });
        // 页面渲染完成
    },
    addMember: function (userInfo) {
        var $this = this;
        app.util.request({
            url: 'entry/wxapp/AddMember',
            data: {
                "m": 'nets_haojk',
                "from_uid": this.data.from_uid,
                "nickname": userInfo.nickname,
                "avatar": userInfo.avatar,
                "city": userInfo.city,
                "country": userInfo.country,
                "province": userInfo.province,
                "gender": userInfo.gender,
            },
            method: 'post',
            success: function (response) {
                //首页存储会员对象到缓存，其他页面直接读取缓存即可
                wx.setStorage({
                    key: "memberinfo",
                    data: response.data.data
                });
                $this.setData({
                    memberid: response.data.data.memberid,
                    datalock: false
                });
                //取会员对象的方式
                wx.getStorage({
                    key: 'memberinfo',
                    success: function (res) {
                    }
                });
                $this.initdetail();
            }
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

        var that = this;
        that.setData({
            showLoading: true
        });
        

    },
    initdetail:function(){
      var that=this;
      var $this=this;
      //判断是否显示约补
      //判断是否先显示补助
      var total_commission = 0;
      var memberinfo = wx.getStorageSync('memberinfo');
      $this.setData({ memberinfo: memberinfo});
      var globalset = wx.getStorageSync('globalset');
    
      if (memberinfo.type == 0) {
        if (globalset.isopen_subsidy == 0) {
          $this.setData({
            isshow_subsidy: false
          });
        }
      
        var total_commission = parseFloat(globalset.subsidy);
        this.setData({
          commission: total_commission,
          datalock: false
        });
      }

      if (globalset.isshow_subsidy == 0) {
        this.setData({
          isshow_subsidy: false
        });
      }
   
      if (that.data.skuId != 0 && that.data.from_uid != 0) {
    
        app.util.request({
          url: 'entry/wxapp/Getcache',
          data: {
            m: "nets_haojk",
            skuId: that.data.skuId
          },
          method: 'post',
          success: function (response) {
            that.setData({
              showLoading: false,
              good: response.data.data
            });
            //计算约补
            //var globalset = wx.getStorageSync('globalset');
            //var memberinfo = wx.getStorageSync('memberinfo');
            var total_commission = 0;
          
            if (memberinfo.type == 0) {
              if (globalset.isopen_subsidy == 0) {
                $this.setData({
                  isshow_subsidy: false
                });
              }
           
              total_commission = parseFloat(globalset.subsidy);
              $this.setData({
                commission: total_commission,
                datalock: false
              });
            }
            //计算总的佣金比例
            if(memberinfo.type==1){
            for (var i = 0; i < globalset.memberlevel.length; i++) {
            
              if (globalset.memberlevel[i].type == memberinfo.type && globalset.memberlevel[i].name == memberinfo.level) {
                var total_commission = parseFloat(globalset.memberlevel[i].myteam_credit2) + parseFloat(globalset.memberlevel[i].myleader1_credit2) + parseFloat(globalset.memberlevel[i].myleader2_credit2);
                //换成只取1级佣金的比例
                total_commission = parseFloat(globalset.memberlevel[i].myteam_credit2);
               
              }
            }
            }
          
            if (total_commission == 0) {
              response.data.data.wlPrice_after1 = 0;
            } else {
              response.data.data.wlPrice_after1 = response.data.data.wlPrice_after * (response.data.data.wlCommissionShare / 100) * (total_commission / 100)*0.9;
            

            }

            if (response.data.data.wlPrice_after1 == null || response.data.data.wlPrice_after1 == "null") {
              response.data.data.wlPrice_after1 = 0;
            }

            if (response.data.data.wlPrice_after1 > 0)
              response.data.data.wlPrice_after1 = parseFloat(response.data.data.wlPrice_after1).toFixed(2);
            //res.data.wlPrice_after1 = wlPrice_after1;
          

            that.setData({ good: response.data.data });
            that.lookDetail();
          
          }
        });

      } else {
        
        wx.getStorage({
          key: 'good',
          success: function (res) {
            //计算约补
            var globalset = wx.getStorageSync('globalset');
            var memberinfo = wx.getStorageSync('memberinfo');
            var total_commission = 0;
            //取会商品对象
            if (memberinfo.type == 0) {
              if (globalset.isopen_subsidy == 0) {
                $this.setData({
                  isshow_subsidy: false
                });
              }
             
              var total_commission = parseFloat(globalset.subsidy);
              $this.setData({
                commission: total_commission,
                datalock: false
              });
            }
            //计算总的佣金比例
            if (memberinfo.type == 1) {
              for (var i = 0; i < globalset.memberlevel.length; i++) {
                
                if (globalset.memberlevel[i].type == memberinfo.type && globalset.memberlevel[i].name == memberinfo.level) {
                  var total_commission = parseFloat(globalset.memberlevel[i].myteam_credit2) + parseFloat(globalset.memberlevel[i].myleader1_credit2) + parseFloat(globalset.memberlevel[i].myleader2_credit2);
                  //换成只取1级佣金的比例
                  total_commission = parseFloat(globalset.memberlevel[i].myteam_credit2);
                
                  
                }
              }
            }
          
            if (total_commission == 0) {
              res.data.wlPrice_after1 = 0;
            } else {
              res.data.wlPrice_after1 = res.data.wlPrice_after * (res.data.wlCommissionShare / 100) * (total_commission / 100)*0.9;
              


            }

            if (res.data.wlPrice_after1 == null || res.data.wlPrice_after1 == "null") {
              res.data.wlPrice_after1 = 0;
            }

            if (res.data.wlPrice_after1 > 0)
              res.data.wlPrice_after1 = parseFloat(res.data.wlPrice_after1).toFixed(2);
            //res.data.wlPrice_after1 = wlPrice_after1;
         

            that.setData({ good: res.data });
            //加载商品详情
            if (that.data.good) {
              that.lookDetail();
            }

       
            wx.setStorage({
              key: 'searchkey',
              data: res.data.skuName
            });


            res.data.m = "nets_haojk";


            app.util.request({
              url: 'entry/wxapp/Unionurl',
              data: {
                m: 'nets_haojk',
                from_uid: that.data.from_uid,
                skuid: res.data.skuId,
                couponUrl: res.data.couponList
              },
              method: 'post',
              success: function (response) {
              
                if (response.data.data == null) {
                  wx.showToast({
                    title: '购买链接生成错误',
                    icon: 'loading',
                    duration: 2000
                  });
                } else {
                  that.setData({
                    buyurl: response.data.data.data
                  });
                  res.data.buyurl = response.data.data.data;
                  app.util.request({
                    url: 'entry/wxapp/Savecache',
                    data: res.data,
                    method: 'post',
                    success: function (response) {
                
                      that.setData({
                        showLoading: false
                      });
                    }
                  });
               
                }
              }
            });
          }
        });
      }
    },
    buyjd: function () {

    },


    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
    onShareAppMessage: function (res) {
        if (res.from === 'button') {
            // 来自页面内转发按钮
           
        }
        var globalset = wx.getStorageSync('globalset');
        var memberinfo = wx.getStorageSync('memberinfo');
        var total_commission = 0;
        //计算总的佣金比例
        for (var i = 0; i < globalset.memberlevel.length; i++) {
           
          if (globalset.memberlevel[i].type == memberinfo.type && globalset.memberlevel[i].name == memberinfo.level) {
            var total_commission = parseFloat(globalset.memberlevel[i].myteam_credit2) + parseFloat(globalset.memberlevel[i].myleader1_credit2) + parseFloat(globalset.memberlevel[i].myleader2_credit2);
            //换成只取1级佣金的比例
            total_commission = parseFloat(globalset.memberlevel[i].myteam_credit2);
           
            
           
          }
        }
     
        if (total_commission == 0) {
            this.data.good.wlPrice_after1 = this.data.good.discount;
        } else {
          this.data.good.wlPrice_after1 = this.data.good.wlPrice_after * (this.data.good.wlCommissionShare / 100)* (total_commission / 100)*0.9;
         


        }

        if (this.data.good.wlPrice_after1 == null || this.data.good.wlPrice_after1=="null"){
          this.data.good.wlPrice_after1=0;
        }

        if (this.data.good.wlPrice_after1 > 0)
           this.data.good.wlPrice_after1 =parseFloat(this.data.good.wlPrice_after1).toFixed(2)
         

        var title1 = "【原价】￥" + this.data.good.wlPrice + "【券后价】￥" + this.data.good.wlPrice_after + " " + this.data.good.skuName;

        return {
            title: title1,
            path: '/haojk/pages/detail/index?skuId=' + this.data.skuId + '&from_uid=' + this.data.memberid,
            success: function (res) {
                // 转发成功
            },
            fail: function (res) {
                // 转发失败
            }
        }
    },
    openSearch: function () {
       
        wx.showModal({
            title: '确认操作',
            content: '是否使用 超级搜索 查询本商品更多优惠',
            success: function (res) {
                if (res.confirm) {
                  wx.navigateTo({
                        url: "../search/index",

                    })
                } else if (res.cancel) {
                  
                }
            }
        })
    },




    closeModal: function () {
        var $that = this;
      
        var globalset = wx.getStorageSync('globalset');
        var buyurl = $that.data.buyurl;
        if (buyurl==""){
          return;
        }
        if (globalset.goodsqrtype==2){
          //buyurl = '/haojk/pages/detail/index?skuId=' + this.data.skuId + '&from_uid=' + this.data.memberid;
        }
        app.util.request({
            url: 'entry/wxapp/GetGoodPoster',
            data: {

                m: 'nets_haojk',
                skuId:$that.data.good.skuId,
                skuName:$that.data.good.skuName,
                skuDesc: $that.data.good.skuDesc,
                materiaUrl: buyurl,
                picUrl: $that.data.good.picUrl,
                wlPrice: $that.data.good.wlPrice,
                wlPrice_after:$that.data.good.wlPrice_after,
                discount: $that.data.good.discount 
                
            },
            method: 'post',
            success: function (response) {
             
                if(response.data.data){
                  var imgpath = response.data.data.res+"?t="+new Date().getMinutes();
                    $that.setData({
                      imgPoster: imgpath
                    })
                    wx.previewImage({
                      current: imgpath,
                      urls: [imgpath],//内部的地址为绝对路径
                        fail: function () {
                          
                        },
                        success:function(){

                        },
                        complete: function () {
                           
                            
                        },
                    })
                }
               
            }
        });

        if ($that.data.showMotal) {
          
            $that.setData({
                showMotal: false
            })
           
        } else {
           
            $that.setData({
                showMotal: true
            })
           
        }

    
    },
    //返回上一页
    backPrev:function(){
        wx.navigateBack({
            delta: 1
        })
    },
    lookDetail:function(){
        var $that = this;
        app.util.request({
            url: 'entry/wxapp/GetGoodsDetailJD',
            data: {
                m: 'nets_haojk',
                skuid: $that.data.good.skuId,
               
            },
            method: 'post',
            success: function (response) {
            
              if (!response.data.data){
                return;
              }
              var jdurl = "https://" + response.data.data;
              wx.request({
                url: jdurl, //仅为示例，并非真实的接口地址
                dataType: "jsonp",
                charset:"GBK",
                data: {
                },
                header: {
                  //'content-type': 'text/html;charset=gbk', // 默认值
                  "chartset":"GBK",
                  "scriptCharset": 'GBK',
                },
                success: function (res) {
                 
                  var detail = JSON.parse(res.data).content;
                  //var article = detail.replace(/[ ]/g, "");
                  var article = detail.replace(/[\r\n]/g, "");//去掉回车换行

                  //article = article.replace(/<span style= /g, "  <span style=\" display:none  ");
                  
                  article = article.replace(/data-lazyload/g, "  src");
                  article = article.replace(/style/g, " style");
                  article = article.replace(/id/g, " id");
                  article = article.replace(/alt/g, " alt");
                  article = article.replace(/name/g, " name");
                  article = article.replace(/title/g, " title");
                  article = article.replace(/class/g, " class");
                  //article = article.replace(" ", "");
                  //article = $that.data.article;
                  article = WxParse.wxParse('article', 'html', article, $that);

                  $that.setData({ "article": article });
                }
              })
            }
        });
    },
    lookDetailJd: function (jdurl) {
      app.util.request({
        url: jdurl,
        dataType: "jsonp",
        data: {
        },
        method: 'post',
        success: function (response) {
          if (response.data.data == "") {
            return;
          }
          // $that.setData({ 'isDetailShow': false });
        
          var article1 = JSON.parse(response.data.data);
         
          var article = article1.content;
          //article = article.replace(/\ +/g, "");//去掉空格
          article = article.replace(/[ ]/g, "");
          article = article.replace(/[\r\n]/g, "");//去掉回车换行


          article = article.replace(/data-lazyload/g, " src");
          article = article.replace(/style/g, " style");
          //article = article.replace(" ", "");
          //article = $that.data.article;
          article = WxParse.wxParse('article', 'html', article, $that);

          $that.setData({ "article": article });



        }
      });
    },
    lookDetail2:function(){
      
        var $that = this;
        if ($that.data.isDetailContentShow){
           
            $that.setData({"isDetailContentShow":false})
        }else{
            $that.setData({ "isDetailContentShow":true })
            $that.setData({ "isDetailShow": false })
           
        }
    },
    openQuanBox:function(){
        if (wx.canIUse('web-view')) {
          
            wx.navigateTo({
                 url: "../webview/index",

            })
        } else {
            wx.showModal({
                title:'提示',
                content:'当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试',
                success: function (res) {
                
                }
            })
        }  
    }
    
})